<?php
define('ENV', 'test');
date_default_timezone_set('PRC');
switch (ENV) {
    case 'dev':
        define('SERVER', '127.0.0.1');
        define('PORT', '3307');
        define('DBNAME', 'snap_shop');
        define('DBUSER', 'root');
        define('PWD', 'usbw');
        break;
    case 'test':
        define('SERVER', '127.0.0.1');
        define('PORT', '3306');
        define('DBNAME', 'snap_shop');
        define('DBUSER', 'root');
        define('PWD', 'jh!@#2016');
        break;
    case 'prod':
        define('SERVER', '127.0.0.1');
        define('PORT', '3306');
        define('DBNAME', 'snap_shop');
        define('DBUSER', 'root');
        define('PWD', 'jh!@#2016');
        break;
    default:
        break;
}
require_once('lib/PHPMailer-master/PHPMailerAutoload.php');
//##########################################

$mailInfos = getMailInfo();
foreach ($mailInfos as $mail) {
    $send_data['order_sn'] = $mail['order_sn'];
    $send_data['to_email'] = $mail['to_email'];

    $prod = getProdInfoById($mail['product_id']);
    $send_data['product_name'] = $prod['products_name'];

    $order = getOrderInfoById($mail['order_sn']);
    $send_data['phone'] = $order['phone'];
    $send_data['addr'] = $order['addr'];

    $kd = getKdInfoById($mail['order_sn']);
    if (!$kd) {
        $send_data['pz_days'] = "5-7";
    } else {
        $send_data['pz_days'] = $kd['pz_days'];
    }
    send($send_data);
}

function getProdInfoById($pid)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_products where products_id=$pid";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getOrderInfoById($sn)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_userinfo where order_sn='$sn'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getKdInfoById($sn)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_kd where order_sn='$sn'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getMailInfo()
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_mailinfo where status=1";
    $sth = $dbh->query($sql);
    $rows = $sth->fetchAll();
    return $rows;
}


function updateMailStatus($sn)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "update yg_mailinfo set `status`=0 where `order_sn`='$sn'";
    $ret = $dbh->exec($sql);
    return $ret;
}

function writeLog($content, $type)
{
    $logfile = dirname(__FILE__) . '/logs/debuglog_' . date('Ymd') . '_' . $type . '.txt';
    if (!file_exists(dirname($logfile))) {
        @mkdir(dirname($logfile), 0777, true);
    }
    error_log(date("[Y-m-d H:i:s]") . " -[" . $type . "] :" . $content . "\n", 3, $logfile);
}


function send($data)
{
    try {
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->SMTPDebug = false;
        $mail->CharSet = 'UTF-8'; //设置邮件的字符编码，这很重要，不然中文乱码
        $mail->SMTPAuth = true; //开启认证
        $mail->SMTPSecure = "ssl";
        $mail->Port = 465;
        $mail->Host = "smtp.exmail.qq.com";
        $mail->Username = "edit-product@bszmail.com";
        $mail->Password = "Qipaoxian001";
        $mail->From = "edit-product@bszmail.com";
        $mail->FromName = "JIAHONG";
        $to = trim($data['to_email']);
        $mail->addAddress($to);
        $mail->Subject = "團購商品確認信息";
        $mail->Encoding = "base64";

        $content = "<p>您團購的訂單編號：" . $data['order_sn'] . "</p>";
        $content .= "<p>您團購的商品：" . $data['product_name'] . "</p>";
        $content .= "<p>您預留的電話：" . $data['phone'] . "</p>";
        $content .= "<p>您預留的地址：" . $data['addr'] . "</p>";
        if (!isset($data['pz_days'])) {
            $data['pz_days'] = "5-7";
        }
        $content .= "<h3>商品預計在" . $data['pz_days'] . "個工作日送到，請耐心等待，祝您購物愉快！</h3>";
        $content .= "<h6>如需查詢商品運送詳情，請登錄黑貓宅急便(http://www.t-cat.com.tw/)訂單查詢頁面</h6>";
        $mail->Body = $content;
        $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示，可以省略
        $mail->WordWrap = 80; // 设置每行字符串的长度
        $mail->isHTML(true);
        $mail->send();
        updateMailStatus($data['order_sn']);
        writeLog("订单号：" . $data['order_sn'] . "邮件发送成功", "INFO");
    } catch (phpmailerException $e) {
        writeLog($e->getMessage(), "ERROR");
        throw $e;
    }
}




